#!/bin/bash

set -e

### need to install jq at the bastion server first (sudo apt install jq)

BASITION_HOST=$(sh ./files/const.sh "$(pwd)/files/manifest/variable.json" "host.bastion.ip" | sed 's/\"//g' )

token(){
  ssh ubuntu@$BASITION_HOST /bin/sh << EOF
    set -v
    secname=\$(kubectl -n kube-system get secret | grep admin-user | awk '{print \$1}')
    kubectl -n kube-system describe secret \$secname
EOF
}

test(){
	ssh ubuntu@$BASITION_HOST /bin/bash << EOF
	cd files
	 kubectl apply -f ./manifest/service-deployment-files/tws-user-center.yml
EOF
}

copy_files_to_bastion(){
	scp -r ./files ubuntu@$BASITION_HOST:/home/ubuntu/files/
}
setup_env(){
	ssh ubuntu@$BASITION_HOST /bin/bash << EOF
			cd files
			sh setup_env.sh
EOF
}
setup_k8s(){
	ssh ubuntu@$BASITION_HOST /bin/bash << EOF
			cd files
			sh setup_k8s.sh
EOF
}

deploy_services(){
	ssh ubuntu@$BASITION_HOST /bin/bash << EOF
			cd files
			sh deploy_services.sh
EOF
}

case $1 in
	login)
		ssh ubuntu@$BASITION_HOST
		;;
	token)
		token
		;;
	copy_files)
		copy_files_to_bastion
		;;
	setup_env)
		setup_env
		;;
	setup_k8s)
		setup_k8s
	    ;;
	deploy_services)
    deploy_services
      ;;

	all)
		copy_files_to_bastion
		setup_env
		setup_k8s
		deploy_services	
		;;
	test)
	   test
	    ;;
	*)
		echo "Usage: ./remote [login|token|setup_env|setup_k8s|deploy_services]"
		exit 1
		;;
esac